import 'dart:async';

import 'package:flutter/material.dart';

/// Extension on the [num]
extension JMNumExtension on num {
  /// JMNumExtension: Create Duration with days.
  ///
  /// Example:
  /// ```dart
  /// 2.days
  /// ```
  Duration get days => Duration(hours: (this * Duration.hoursPerDay).round());

  /// JMNumExtension: Create Duration with hours.
  ///
  /// Example:
  /// ```dart
  /// 2.hours
  /// ```
  Duration get hours => Duration(minutes: (this * Duration.minutesPerHour).round());

  /// JMNumExtension: Create Duration with milliseconds.
  ///
  /// Example:
  /// ```dart
  /// 2.milliseconds
  /// ```
  Duration get milliseconds => Duration(microseconds: (this * 1000).round());

  /// JMNumExtension: Create Duration with minutes.
  ///
  /// Example:
  /// ```dart
  /// 2.minutes
  /// ```
  Duration get minutes => Duration(seconds: (this * Duration.secondsPerMinute).round());

  /// JMNumExtension: Create Duration with seconds.
  ///
  /// Example:
  /// ```dart
  /// 2.seconds
  /// ```
  Duration get seconds => Duration(milliseconds: (this * 1000).round());

  /// JMNumExtension: Create Future.delay().
  ///
  /// Example:
  /// ```dart
  /// await 2.delay()
  /// ```
  Future delay([FutureOr Function()? callback]) async {
    return Future.delayed(
      Duration(milliseconds: (this * 1000).round()),
      callback,
    );
  }

  /// JMNumExtension: Create SizedBox with height.
  ///
  /// Example:
  /// ```dart
  /// Column(children: [Container(), 10.heightBox, Container()])
  /// ```
  Widget get heightBox => SizedBox(height: double.tryParse(toString()));

  /// JMNumExtension: Create SizedBox with width.
  ///
  /// Example:
  /// ```dart
  /// Row(children: [Container(), 10.widthBox, Container()])
  /// ```
  Widget get widthBox => SizedBox(width: double.tryParse(toString()));

  /// JMNumExtension: Num to DateTime.
  ///
  /// Example:
  /// ```dart
  /// 1000000000000.toDateTime
  /// ```
  DateTime get toDateTime {
    var len = toString().length;
    if (len < 10) return DateTime.now();

    var longInt = len == 10 ? this * 1000 : this;
    return DateTime.fromMillisecondsSinceEpoch(longInt.toInt());
  }
}
